#include "book_form.h"
#include "ui_book_form.h"

Book_Form::Book_Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Book_Form)
{
    ui->setupUi(this);
    ui->pushButton_Close->setToolTip("关闭电子书任务");                    //设置关闭按钮提示
    ui->pushButton_OpenBook->setToolTip("打开电子书");                   //设置打开提示
    ui->pushButton_set_color->setToolTip("设置文字颜色");                 //设置颜色提示
    ui->pushButton_set_font->setToolTip("选择字体");                        //设置字体提示
    ui->pushButton_background->setToolTip("设置背影颜色");                //设置背景提示
    ui->pushButton_Save_mark->setToolTip("保存标签");                   //设置保存标签提示
    ui->pushButton_open_mark->setToolTip("打开标签");                       //设置打开标签提示
    ui->pushButton_set_color->setEnabled(false);                        //关闭按钮置灰
    ui->pushButton_set_font->setEnabled(false);                         //设置颜色置灰
    ui->pushButton_background->setEnabled(false);                       //设置背景置灰
    ui->pushButton_Save_mark->setEnabled(false);                        //设置保存置灰
    ui->pushButton_open_mark->setEnabled(false);                        //设置打开按钮置灰
    ui->label->setStyleSheet("border-image:url(:/Main/Images/BackGround/Book_Back.png)");               //设置电子书背景
    ui->textEdit->setStyleSheet("background-color:transparent");                                    //设置透明
    ui->pushButton_OpenBook->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Openbook.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Openbookon.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Openbook.png);}");            //设置打开书样式表
    ui->pushButton_set_color->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Setcolor.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Setcoloron.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Setcolor.png);}");               //设置设置字体颜色样式表
    ui->pushButton_set_font->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Setfont.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Setfonton.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Setfont.png);}");                   //设置字体样式表
    ui->pushButton_background->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Backgeound.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Backgeoundon.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Backgeound.png);}");        //设置背景颜色样式表
    ui->pushButton_Save_mark->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Booktip.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Booktipon.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Booktip.png);}");                  //设置保存书签样式表
    ui->pushButton_open_mark->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Openbooktip.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Openbooktipon.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Openbooktip.png);}");      //设置打开书签样式表
    ui->pushButton_Close->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Close.png);}QPushButton:hover{border-image:url(:/Book/Images/Book/Closeon.png);}QPushButton:pressed{border-image:url(:/Book/Images/Book/Close.png);}");            //设置关闭按钮样式表

    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(post_book_money()));
}

Book_Form::~Book_Form()
{
    timer->stop();
    delete ui;              //删除界面
}
/*
 * 读取文件
 */
void Book_Form::openfile(QString filename, int x)
{
    QFile file;                             //新建文件对象
    file.setFileName(filename);             //设置文件名字
    if(file.isOpen())                   //判断文件是否打开
    {
        file.close();                   //如果文件打开，则关闭文件
    }
    file.open(QIODevice::ReadOnly);         //以只读方式打开文件
    QTextStream stream(&file);              //读取文字流
    ui->textEdit->setPlainText(stream.readAll());           //读取所有，并显示到 文本编辑器中
    file.close();               //关闭文件
    QScrollBar *n = ui->textEdit->verticalScrollBar();          //设置纵向滚动条的位置
    n->setValue(x);
    ui->pushButton_set_color->setEnabled(true);                 //设置颜色激活
    ui->pushButton_set_font->setEnabled(true);                      //设置字体激活
    ui->pushButton_background->setEnabled(true);                //设置背景颜色激活
    ui->pushButton_Save_mark->setEnabled(true);                     //设置保存标签激活
    ui->pushButton_open_mark->setEnabled(true);                         //设置打开标签激活
}
/*
 * 打开文件
 */
void Book_Form::on_pushButton_OpenBook_clicked()
{
    filepath = QFileDialog::getOpenFileName(this,"Open File","//Books","All (*.*);;text (*.txt);;c文件 (*.c *.cc *.cpp)PDF (*.pdf);;word (*.doc)");      //打开文件
    openfile(filepath,0);   //调用读取文件函数
    timer->start(180000);
}
/*
 * 关闭电子书函数
 */
void Book_Form::on_pushButton_Close_clicked()
{
    emit close_form();              //触发关闭按钮槽函数
    this->close();              //调用关闭函数
}
/*
 * 设置字体颜色
 */
void Book_Form::on_pushButton_set_color_clicked()
{
    QColor color = QColorDialog::getColor();                    //获取颜色
    if(color.isValid())
    {
        QScrollBar *the_vertical = ui->textEdit->verticalScrollBar();                       //新建滚动条变量
        int temp = the_vertical->value();               //获取当前滚动条值
        ui->textEdit->selectAll();                  //全选
        ui->textEdit->setTextColor(QColor(color));          //设置颜色
        the_vertical->setValue(temp);               //滚动条赋值
        ui->textEdit->update();                     //界面刷新
    }
    else
    {
        QMessageBox::warning(this,"Warning","设置字体颜色失败",QMessageBox::Yes);               //弹出警告窗口
    }
}
/*
 * 设置字体类型
 */
void Book_Form::on_pushButton_set_font_clicked()
{
    bool ok;                            //新建bool类型变量
    QFont font = QFontDialog::getFont(&ok);                         //获取字体
    if(ok)
    {
        ui->textEdit->setFont(QFont(font));                 //设置字体
    }
    else
    {
        QMessageBox::warning(this,"Warning","设置字体失败",QMessageBox::Yes);             //弹出警告窗口
    }

}
/*
 * 设置背景颜色
 */
void Book_Form::on_pushButton_background_clicked()
{
    QColor color = QColorDialog::getColor();                //获取颜色
    if(color.isValid())
    {
        ui->textEdit->setTextBackgroundColor(QColor(color));                    //设置颜色

    }
    else
    {
        QMessageBox::warning(this,"Warning","设背景颜色失败",QMessageBox::Yes);                    //弹出警告窗口
    }
}
/*
 * 保存标签
 */
void Book_Form::on_pushButton_Save_mark_clicked()
{
    QScrollBar *SB = ui->textEdit->verticalScrollBar();             //新建滚动条变量
    int n = SB->value();                            //获取当前滚动条值
    QFile file("./ebookMark.TONG");                 //打开书签文件
    file.open(QIODevice::WriteOnly);                        //只写方式
    QDataStream save(&file);
    save<<QString(filepath);            //保存文件
    save<<(qint32)n;
    QMessageBox::information(this, "Save Book Mark", "Save Book Mark success!");                //弹出提示窗口
}
/*
 * 打开标签
 */
void Book_Form::on_pushButton_open_mark_clicked()
{
    int x = 0;                              //定义变量
    QFile file("./ebookMark.TONG");         //打开文件
    file.open(QIODevice::ReadOnly);             //只读方式
    QDataStream open(&file);
    open>>filepath>>x;                  //读取数据
    openfile(filepath, x);              //函数调用
}

void Book_Form::post_book_money()
{
    emit book_moeny(21);
}

void Book_Form::on_label_linkActivated(const QString &link)
{

}
